﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BesAsm.Emgaats.Maintenance
{
    internal class Field
    {
        private HashSet<object> _validValues = null;
        private double? _maxValue = null;
        private bool _maxInclusive = true;
        private double? _minValue = null;
        private bool _minInclusive = true;

        public Field(string fieldName)
        {
            Name = fieldName;
            FieldType = typeof(string);
            SourceField = fieldName;
            Ignore = false;
            Hidden = false;
            UseSpecialLogic = false;
            UseDefault = false;
            DefaultValue = DBNull.Value;
            UseValueMapping = false;
            AutoUpdate = false;
            ValueMapping = new Dictionary<object, object>();
        }

        public string Name { get; set; }
        public Type FieldType { get; set; }
        public string SourceField { get; set; }
        public bool Ignore { get; set; }
        public bool Hidden { get; set; }
        public bool UseSpecialLogic { get; set; }
        public bool UseDefault { get; set; }
        public object DefaultValue { get; set; }
        public bool UseValueMapping { get; set; }
        public bool AutoUpdate { get; set; }
        public bool Validate { get; set; }
        public Dictionary<object, object> ValueMapping { get; set; }

        public void SetValidValues(List<object> validValues)
        {
            if (validValues != null)
                _validValues = new HashSet<object>(validValues.Distinct());
            else
                _validValues = null;
        }

        public void AddValidValue(object value)
        {
            if (_validValues == null)
                _validValues = new HashSet<object>();
            if(!_validValues.Contains(value))
                _validValues.Add(value);
        }

        public void SetMaxValue(object max, bool inclusive)
        {
            _maxValue = Convert.ToDouble(max);
            _maxInclusive = inclusive;
        }

        public void SetMinValue(object min, bool inclusive)
        {
            _minValue = Convert.ToDouble(min);
            _minInclusive = inclusive;
        }

        public bool IsValidValue(object value)
        {
            if (_validValues != null)
            {
                return _validValues.Contains(value);
            }
            else
            {
                // HACK: We assume DBNull is valid for now, though this should be tested explicitly.
                if (value == DBNull.Value)
                    return true;

                if (_maxValue.HasValue)
                {
                    if (_maxInclusive)
                    {
                        return _maxValue >= Convert.ToDouble(value);
                    }
                    else
                    {
                        return _maxValue > Convert.ToDouble(value);
                    }
                }

                if (_minValue.HasValue)
                {
                    if (_minInclusive)
                    {
                        return _minValue <= Convert.ToDouble(value);
                    }
                    else
                    {
                        return _minValue < Convert.ToDouble(value);
                    }
                }
            }

            return true;
        }
    }
}
